/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.managers;

import javax.swing.text.JTextComponent;
import org.balisunrise.application.Application;
import org.balisunrise.common.Basic;
import org.balisunrise.common.Binding;
import org.balisunrise.common.BindingGroup;
import org.balisunrise.common.ComponentManager;
import org.balisunrise.common.Converter;
import org.balisunrise.common.ConverterException;

/**
 *
 * @author glauco
 */
public abstract class JTextFieldComponentManager<T,V extends JTextComponent>
        implements ComponentManager<T, V>{

    @Override
    public void clearComponentValue(V component) {
        if(component == null)
            return ;
        component.setText("");
    }

    @Override
    public Object readValue(V component) {
        if (null == component)
            return null;

        return component.getText();
    }

    @Override
    public T readConvertedValue(V component, boolean nullForEmpty)
            throws ConverterException {

        Object obj = readValue(component);

        if (obj == null)
            return null;

        String value = (String)obj;

        if(nullForEmpty && value.length() == 0)
            return null;

        try{
            return getConverter(nullForEmpty).convertReverse(value);
        }catch(Exception e){
            throw new ConverterException(e.getMessage());
        }
    }

    @Override
    public void insertValue(T propertyValue, V component)
            throws ConverterException {

        insertConvertedValue(
                getConverter(false).convertForward(propertyValue),component);
    }

    @Override
    public void insertConvertedValue(Object propertyValue, V component) {

        if (component == null)
            return ;

        component.setText((String) propertyValue);
        
    }

    @Override
    public abstract Converter<T, String> getConverter(boolean bullForEmpty);

    @Override
    public Binding createBinding(BindingGroup bindingGroup, Object source,
            String property, V component, boolean nullForEmpty, Basic owner) {

        return (Binding) Application.get().getBindingBuilder().createBinding(
                bindingGroup,
                source,
                property,
                component,
                "text", // propriedade do componente a fazer o bind
                this,
                nullForEmpty,
                owner);
    }

}
